<template>
  <div class="logoSearch flex-y-center flex-grow-1">
    <div class="maxWidth flex-y-center">
      <div class="logobox" v-show="showLogo">
        <a :href="homeUrl"><img style="width: 140px" v-src="'/static/img/z_logo.png'" alt=""></a>
      </div>
      <div class="searchbox">
        <form @submit.prevent="subMit" action=".">
          <div class="search_contain_left">
            <div class="search_contain_box fl">
              <i class="com_search_icon"></i>
              <input ref="searchInfo"
                     type="text" @keyup.stop="keyup"
                     v-model="inputVal"
                     @focus="onFocus"
                     @blur="onBlur"
                     class="search_info txt_focus"
                     value="粘贴宝贝标题或输入关键词找优惠券"
                     lang="粘贴宝贝标题或输入关键词找优惠券"
                     autocomplete="off"
                     placeholder="粘贴宝贝标题或输入关键词找优惠券">
            </div>
            <input type="submit" class="fr search_btn" value="找券">
            <ul v-show="inputVal && showSearchResoult" class="search_items" id="search_items">
              <li :class="{'addclass':addclassIndex==index}"  @click="listToSearch(item)" v-for="(item,index) in searchList">
                <a href="javascript:void(0)" >{{item[0]}}</a>
              </li>
            </ul>
          </div>
        </form>
        <div v-show="showhotKeyWord" class="hot_search_box clearfix">
          <ul class="hot_search_list fl ">
            <li @click="toGoSearch(item)" v-for="(item,index) in hotSearchList"><a
              href="javascript:void(0);">{{item}}</a></li>
          </ul>
          <a class=" sq_help fr" target="_blank" href="/html/souhuiquan.html"><i></i><span>找券帮助</span></a>
        </div>
      </div>
      <div v-show="showErweima" class="ewmBox"></div>

    </div>
  </div>
</template>
<script>
  import jsonp from 'jsonp'
  export default {
    props: {
      propsValue: {
        type: String,
        default: ''
      },
      showLogo:{
        type:Boolean,
        default:true
      },
      showErweima:{
        type:Boolean,
        default:true
      },
      showhotKeyWord:{
        type:Boolean,
        default:true,
      },
      // showSearchResoult:{
      //   type:Boolean,
      //   default:true
      // }
    },
    data() {
      return {
        inputVal: '',//双向数据绑定
        hotSearchList: ['袜子', '拖鞋', '鞋', '睡衣', '保温杯', '耳机', '帽子', '加湿器'],
        searchList:[],
        showSearchResoult:false,
        saveInputVal:'',
        homeUrl:`${cdnBase}/html/index.html`,
        addclassIndex:-1,
      }
    },
    mounted(){
    },
    watch: {
      propsValue(val) {
        this.inputVal = val;
        this.showSearchResoult=false;
      },
      inputVal(val) {
        if(val){
          this.$emit('on-change', val);
        }else{
          this.searchList=[];
        }

      }
    },
    methods: {
      toGoSearch(item) {
        window.location.href = `${cdnBase}/html/souhuiquan.html?key=${item}`
      },
      listToSearch(item){
        if(item.length>0){
          window.location.href = `${cdnBase}/html/souhuiquan.html?key=${item[0]}`
        }

      },
      subMit() {
        if (this.inputVal) {
          this.$emit('on-submit', this.inputVal)
        } else {
          this.$vux.toast.show({type: 'text', text: '搜索关键词不能为空，请重新输入'});
        }
      },
      onFocus(){
       // this.showSearchResoult=true;
      },
      onBlur(){
        setTimeout(()=>{
          this.showSearchResoult=false;
        },200)
      },
      //键盘事件
      keyup(e){
        if(this.inputVal){
          this.showSearchResoult=true;
          let saveInputVal=this.$refs.searchInfo.value;
          console.log(this.$refs.searchInfo.value);
          let itemLength=this.searchList.length;
          let keyCode=e.keyCode;
          //向上，
          if(keyCode==38){
            if(this.addclassIndex<0){
              this.addclassIndex=itemLength;
              this.inputVal=saveInputVal;
            }else if(this.addclassIndex==0){
              this.addclassIndex = itemLength + 1;
              this.inputVal=saveInputVal;
            }
            this.addclassIndex--;
            if(this.addclassIndex>=0 && this.addclassIndex<itemLength){
              this.inputVal=this.searchList[this.addclassIndex][0];
            }
            return false;
          }else if(keyCode==40){
            //向下
            if(this.addclassIndex>=itemLength){
              this.addclassIndex=-1;
              this.inputVal=saveInputVal;
            }
            this.addclassIndex++;
            if(this.addclassIndex>=0 && this.addclassIndex<itemLength){
              this.inputVal=this.searchList[this.addclassIndex][0];
            }
            return false;
          }
          let _this=this;
          if(true){
            jsonp(`https://suggest.taobao.com/sug?code=utf-8&q=${this.inputVal}`,
              (err, res)=>{
                _this.showSearchResoult=true;
                _this.searchList=res.result;
              })
          }
        }
      }
    }
  }
</script>
<style lang="less">
  .logoSearch {
    background: #fff;
    height: 120px;
    .maxWidth {
      > div {
        display: inline-block;
        &.logobox {
        }
        &.searchbox {
          margin-left: 136px;
          position: relative;
          z-index: 100000;
          .search_contain_left {
            width: 497px;
            border: 2px solid #f54e54;
            border-radius: 3px;
            overflow: hidden;
            .search_contain_box {
              padding: 9px;
              padding-right: 0;
              .com_search_icon {
                display: inline-block;
                width: 18px;
                height: 18px;
                vertical-align: middle;
                background: url("../../../static/img/search_icon.png") no-repeat center center;
                background-size: 100% 100%;
              }
              .search_info {
                width: 368px;
                margin-left: 9px;
                font-size: 14px;
                color: #a6a4b2;
                line-height: 18px;
                height: 18px;
                border: 0;
                vertical-align: middle;
                outline-style: none;
              }
            }
            .search_btn {
              width: 85px;
              font-size: 16px;
              text-align: center;
              line-height: 39px;
              border: none;
              color: #fff;
              background-color: #f54e54;
              cursor: pointer;
            }
            .search_items{
              position: absolute;
              top: 40px;
              left: 0;
              width: 411px;
              background-color: #fff;
              border: 1px solid #f54e54;
              z-index: 99;
              li{
                padding-left: 10px;
                line-height: 30px;
                &:hover{
                  background: #fff2f2;
                }
                &.addclass{
                  background: #fff2f2;
                }
                a{
                  display: block;
                  color: #333;
                  font-weight: 700;
                }

              }
            }
          }
          .hot_search_box {
            margin-top: 6px;
            padding-right: 22px;
            .hot_search_list {
              padding-left: 2px;
              li {
                float: left;
                margin-right: 18px;
                a {
                  display: block;
                  color: #757380;
                  &:hover {
                    color: #3366cc;
                  }
                }
              }
            }
            .sq_help {
              color: #3366cc;
              i {
                display: inline-block;
                margin-right: 4px;
                width: 13px;
                height: 13px;
                background: url(https://i.huim.com/content_new/images/site_top1.png?v=0506) no-repeat 0 -153px;
                vertical-align: middle;
              }
              span {
                vertical-align: middle;
              }
            }
          }
        }
        &.ewmBox {
        }
      }

    }
  }
</style>
